package com.maoye.ossp.common.web.taglib;

import java.util.ArrayList;
import java.util.List;

import com.maoye.ossp.sys.base.entity.SysDictDetail;
import com.maoye.ossp.sys.base.utils.SysDictUtils;

/**
 * 自定义checkbox标签java类
 * 
 * @author yanghao
 * 
 */
public class CheckBoxTag extends BaseTag {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String name;
	private String defaultChecked;
	private String id ;
	public String getDefaultChecked() {
		return defaultChecked;
	}

	public void setDefaultChecked(String defaultChecked) {
		this.defaultChecked = defaultChecked;
	}

	private String dictType;
	private String htmlEleProperties;

	public String getHtmlEleProperties() {
		return htmlEleProperties;
	}

	public void setHtmlEle(String htmlEle) {
		this.htmlEleProperties = htmlEle;
	}

	public int doStartTag() {
		try {
			StringBuilder sb = new StringBuilder();
			Object obj = pageContext.getRequest().getAttribute(dictType);
			List<Option> options = new ArrayList<Option>();
			
			List<SysDictDetail> sysDictDetailList = SysDictUtils.getSysDictDetailList(dictType);
			for(SysDictDetail sysDictDetail : sysDictDetailList){
				Option option = new Option();
				option.setOptionName(sysDictDetail.getValue());
				option.setOptionValue(sysDictDetail.getKey());
				options.add(option);
			}
			
			List<String> checkedParams = null;
			if (obj != null) {
				if (obj instanceof List) {
					checkedParams = (List<String>) obj;
				} else {
					checkedParams = new ArrayList<String>();
					checkedParams.add(obj.toString());
				}
			}
			for (Option option : options) {
				String id_ = id + "_" + option.getOptionValue();
				sb.append("<input type=\"checkbox\" ");

				if (name != null) {
					sb.append("name=\"");
					sb.append(name);
					sb.append("\"  ");
				}
				sb.append("value=\"");
				sb.append(option.getOptionValue());
				sb.append("\"  ");
				sb.append("id=\"");
				sb.append(id_);
				sb.append("\"  ");

				if (checkedParams != null) {
					if (checkedParams.contains(option.getOptionValue())) {
						sb.append(" checked ");
					}
				}
				if (defaultChecked != null
						&& defaultChecked.equals(option.getOptionValue())) {
					sb.append(" checked ");
				}
				generateAttribute(sb);
				sb.append(" /> ");
				sb.append("<label for=\"").append(id_).append("\">")
						.append(option.getOptionName())
						.append("</label> &nbsp; ");

				sb.append("\n");
			}
			pageContext.getOut().print(sb.toString());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDictType() {
		return dictType;
	}

	public void setDictType(String dictType) {
		this.dictType = dictType;
	}

}
